from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rm, rmdir, rename
from conan.tools.microsoft import is_msvc
import os


class SpineConan(ConanFile):
    name = "spine"
    description = "2D skeletal animation"
    license = "https://github.com/EsotericSoftware/spine-runtimes/blob/4.1/LICENSE"
    url = "https://github.com/conan-io/conan-center-index"
    homepage = "https://github.com/EsotericSoftware/spine-runtimes.git"
    topics = ("spine", "2d", "skeletal animation")
    package_type = "library"
    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    def export_sources(self):
        copy(self, "CMakeLists.txt.bak", os.path.join(self.recipe_folder), self.export_sources_folder)
        export_conandata_patches(self)

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")

    def layout(self):
        cmake_layout(self, src_folder=".")

    def source(self):
        get(self, **self.conan_data["sources"][self.version],
            destination=self.source_folder, strip_root=True)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

        if is_msvc(self):
            cd = CMakeDeps(self)
            cd.generate()

    def _patch_sources(self):
        apply_conandata_patches(self)
        # replace_in_file(self, os.path.join(self.source_folder, "spine-runtimes-" + self.version, "spine-c", "CMakeLists.txt"), "dist/lib", "lib")
        # replace_in_file(self, os.path.join(self.source_folder, "spine-runtimes-" + self.version, "spine-c", "CMakeLists.txt"), "dist/include", "include")
        replace_in_file(self, os.path.join(self.source_folder, "spine-cpp", "CMakeLists.txt"), "dist/lib", "lib")
        replace_in_file(self, os.path.join(self.source_folder, "spine-cpp", "CMakeLists.txt"), "dist/include", "include/spine")
        rm(self, "CMakeLists.txt", os.path.join(self.source_folder))
        rename(self, os.path.join(self.source_folder, "CMakeLists.txt.bak"), os.path.join(self.source_folder, "CMakeLists.txt"))
        
    def build(self):
        self._patch_sources()
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.set_property("cmake_file_name", "spine")
        self.cpp_info.set_property("cmake_target_name", "spine::spine")
        self.cpp_info.set_property("pkg_config_name", "spine")
        self.cpp_info.libs = ["spine-cpp"]
        
        self.cpp_info.names["cmake_find_package"] = "spine"
        self.cpp_info.names["cmake_find_package_multi"] = "spine"
